module MC.UI {
    export module muiPopPicker {
        /**
         * selectJq 需要绑定的对象字符串
         * list=[{
						value: 'ywj',
						text: '董事长 叶文洁'
					}, {
						value: 'aaa',
						text: '总经理 艾AA'
					}]
         */
        export function bind(selectJq: any, list: any, Change: Function = null) {

            //   loadmuiJsCss();
            let dateP = $(selectJq);
            if (selectJq.vue) {
                let mcname = dateP.attr('mcname');
                selectJq.vue.$watch(mcname, (nval: any, oval: any) => {
                    selectJq.value = nval;
                });
            }

            for (let i = 0; i < dateP.length; i++) {
                let one = dateP[i];
                let oneJq = $(one);
                one.PopPicker = new mui.PopPicker();
                one.PopPicker.setData(list);
                one.setData = (_list) => {
                    list = [];
                    for (let i = 0; i < _list.length; i++) {
                        let li = _list[i];
                        if (typeof(li) == 'string') {
                            list.push({
                                value: li,
                                text: li
                            });
                        } else {
                            list.push(li);
                        }
                    }
                    console.log(list);
                    //list=_list;
                    let mcname = $(one).attr('mcname');
                    one.PopPicker.setData(list);
                    if (list && list[0]) {
                        one.value = list[0].value;
                        selectJq.vue[mcname]=one.value;
                    }
                }
                Object.defineProperty(one, "value", {
                    get: function () {
                        return one._value;
                    },
                    set: function (val) {
                        //   console.log(val);
                        let onedata = getvalueli(val, list);
                        if (onedata) {
                            oneJq.text(onedata.text);
                        }
                        //if (list && list[0] && list[0].value == val)
                        if (!val||'' == val) {///如果是第一个表示默认值
                            dateP.addClass("defaultval");
                        } else {
                            dateP.removeClass("defaultval");
                        }
                        one._value = val;
                    },
                    enumerable: true,
                    configurable: true
                });
                oneJq.on('click', (e: any) => {
                    e.preventDefault();
                    e.stopPropagation();
                    // console.log("stopPropagation");
                    let target = e.target;
                    $(target).focus();
                    if (!target.PopPicker) {
                        /*  target.PopPicker = new mui.PopPicker();
                          target.PopPicker.setData(list);*/
                    }
                    if (target.value) {
                        //  console.log(list);
                        target.PopPicker.pickers[0].setSelectedValue(target.value);
                        /// target.PopPicker.setSelectedValue(target.value);
                    }
                    target.PopPicker.show((val: any) => {
                        let value = val[0].value;
                        $(target).val(value);
                        target.dispatchEvent(new Event('select'));
                        target.dispatchEvent(new Event('change'));
                        if (selectJq.vue) {
                            let mcname = dateP.attr('mcname');
                            Vue.set(selectJq.vue, mcname, value);
                        }

                        Change && Change(value);
                    });

                    return false;
                });
                oneJq.addClass("PopPicker");
            }
        }

        function getvalueli(val: string, list: any) {
            for (let i = 0; i < list.length; i++) {
                let one = list[i];
                if (one.value == val) {
                    return one;
                }
            }
            return null;
        }

        export function showSexSelect(selectJq: string, Change: Function = null) {
            bind(selectJq, [{
                value: '1',
                text: '男'
            }, {
                value: '2',
                text: '女'
            }], Change);
        }


        let isload = false;

        function loadmuiJsCss() {
            /*
            if (isload) {
                return;
            }
            MC.UI.Mui.loadbase(() => {
                MC.UI.Mui.loadPicker();
            });*/

        }
    }
}